/**
 * Created by DevilsEyes on 2016/3/14.
 */
'use strict';

define(['text!path/myself/tmp.html', 'logger', 'layer'], function (page, logger, layer) {

    var render = template.compile(page);

    var apiList = {
        getInfo: api + 'homepage'
    };


    var vm = new Page.creat({
        $id: 'myself',
        $model: {
            data: {},
            $: {},
            init: function () {
                vm.$init({
                    setTitle: '我的'
                });

                //预渲染
                $('#page').html(render(vm.data));

                logger.promise
                    .done(function () {
                        if (!vm.isThisPage())return;
                        vm.data = {
                            userInfo: db.get('userInfo')
                        };
                        $('#page').html(render(vm.data));

                        ex.async(function () {

                            vm.bind$(vm.$);
                            vm.bindEvent(vm.event);

                        });

                    })
                    .error(function () {
                        if (!vm.isThisPage())return;
                        ex.async(function () {

                            vm.bind$(vm.$);
                            vm.bindEvent(vm.event);

                        });
                    });
            },
            event: {

                login: function () {
                    if (logger.isLogin) {
                        vm.init();
                    } else {
                        logger.login({
                            close: true,
                            callback: function () {
                                vm.init();
                            }
                        })
                    }
                },

                checkAndGoto: function (hash) {
                    if (logger.isLogin) {
                        location.hash = hash;
                    } else {
                        logger.login({
                            close: true,
                            callback: function () {
                                location.hash = hash;
                            }
                        })
                    }
                }

            }
        }
    });

    return {
        init: function () {
            vm.init();
        }
    }
});